/*
 * @(#)NoticeAction.java $version 2011. 10. 14
 * 
 * Copyright 2011 NHN Corp. All rights Reserved.
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.hangame.mbl.action.notice;

import java.util.List;

import org.apache.commons.lang.math.NumberUtils;

import com.hangame.mbl.action.SmartCommonBaseAction;
import com.hangame.mbl.bo.notice.NoticeBO;
import com.hangame.mbl.model.notice.Notice;
import com.hangame.mbl.util.CommonUtil;
import com.nhncorp.hancore.action.AlertForward;
import com.nhncorp.lucy.web.metadata.Inject;
import com.nhncorp.pagination.taglib.PaginationInfo;

// TODO: Auto-generated Javadoc
/**
 * The Class NoticeAction.
 * 
 * @author khpark
 */
public class NoticeAction extends SmartCommonBaseAction {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -6793096386401708324L;

	/** The notice bo. */
	private NoticeBO noticeBO;

	/* (non-Javadoc)
	 * @see com.hangame.mbl.action.SmartCommonBaseAction#setNoticeBO(com.hangame.mbl.bo.notice.NoticeBO)
	 */
	@Inject(beanId = "noticeBO")
	public void setNoticeBO(NoticeBO noticeBO) {
		this.noticeBO = noticeBO;
	}

	/* (non-Javadoc)
	 * @see com.hangame.mbl.action.SmartPortalAction#execute()
	 */
	@Override
	public String execute() throws Exception {
		return noticeList();
	}

	/**
	 * Notice list.
	 * 
	 * @return the string
	 * @throws Exception the exception
	 */
	public String noticeList() throws Exception {
		int page = NumberUtils.toInt(params.getString("page"), 1);

		int totCnt = this.noticeBO.getNoticeListCount();
		PaginationInfo paginationInfo = new PaginationInfo("default", request, totCnt);
		paginationInfo.setPageSize(15);

		List<Notice> noticeList = this.noticeBO.getNoticeList(CommonUtil.calcOffset(page, paginationInfo.getPageSize()), paginationInfo.getPageSize());

		setAttribute("noticeList", noticeList);
		setAttribute("paginationInfo", paginationInfo);

		return "noticeList";
	}

	/**
	 * Notice detail.
	 * 
	 * @return the string
	 * @throws Exception the exception
	 */
	public String noticeDetail() throws Exception {
		int nId = NumberUtils.toInt(params.getString("nId"), -1);

		if (nId == -1) {
			//잘못된 요청
			return AlertForward.alertAndBack("not.exist.page");
		}

		Notice notice = this.noticeBO.getNoticeDetail(nId);
		setAttribute("notice", notice);

		return "noticeDetail";
	}
}
